package cn.wsalix.weixin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.status.FlagEnum;
import cn.anlaser.utils.BeanMapper;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.service.UserService;
import cn.wsalix.init.UserInit;
import cn.wsalix.weixin.entity.WxConfig;
import cn.wsalix.weixin.form.WxConfigForm;
import cn.wsalix.weixin.repository.WxConfigRepository;
import cn.wsalix.weixin.service.WxConfigService;

@Service("wxConfigService")
public class WxConfigServiceImpl extends
		SimpleServiceImpl<WxConfigForm, WxConfigRepository, WxConfig> implements
		WxConfigService {
	// private String serverPath = "http://www.jskyme.cn/weixin/%s.do";
	@Autowired
	private WxConfigRepository wxConfigRepository;
	@Autowired
	private UserService userService;
	@Autowired
	private UserInit userInit;
	@Override
	public WxConfigRepository getRepository() {
		return wxConfigRepository;
	}

	@Override
	public String module() {
		return "/weixin/config";
	}

	@Transactional
	@Override
	public WxConfig add(WxConfigForm form) {
		/*SysUser user = userService.findById(form.getUserId());
		form.setUser(user);
		WxConfig wxConfig = super.add(form);
		// wxConfig.setServerUrl(String.format(serverPath, wxConfig.getId()));
		return super.edit(wxConfig);*/
		WxConfig entity = BeanMapper.map(form, getEntityClazz());
		traversalAssign(form, entity);		
		if (form.getUserId() != null && form.getUserId() != 0) {
			SysUser user = userService.findById(form.getUserId());
			entity.setUser(user);
		}
		BaseUser user = userUtils.getUser();
		entity.setCreatedDate(new Date());// DateTime.now());
		entity.setLastModifiedDate(new Date());
		if (user != null) {
			entity.setCreatedBy(user.getId());
		}
		if (entity.getFlag() == null) {// 默认数据，全部有效。新增商品、广告、文章，默认无效，新建时设置
			entity.setFlag(FlagEnum.valid);
		}
		return super.add(entity);
	}

	@Override
	public Map<String, SearchFilter> buildByForm(WxConfigForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getUserId() != null && form.getUserId() != 0) {
			filters.put("user.id", new SearchFilter("user.id", Operator.EQ,
					form.getUserId()));
		}
		if (form.getStatus() != null) {
			filters.put("status",
					new SearchFilter("status", Operator.EQ, form.getStatus()));
		}
		return filters;
	}

	@Override
	public Class<WxConfig> getEntityClazz() {
		return WxConfig.class;
	}

	@Cacheable(value = "wxconfigCache", key = "#wxConfigId")
	@Override
	public WxConfig findByConfigId(Long wxConfigId) {
		return findById(wxConfigId);
	}

	@Override
	public List<WxConfig> findByUser(Long userId) {
		// SysUser admin = userService.findByUsername(adminAccount);
		WxConfigForm form = new WxConfigForm();
		// form.setUserId(admin.getId());

		form.setUserId(userId);
		List<WxConfig> wxConfigs = findByForm(form);
		return wxConfigs;
	}

	@Override
	public WxConfig findByDefault() {
		return findByDefault(userInit.getAdmin().getId());
	}

	@Override
	public WxConfig findByDefault(Long userId) {
		WxConfigForm form = new WxConfigForm();
		form.setDefaultConfig(true);
		form.setCreatedBy(userId);
		List<WxConfig> wxConfigs = findByForm(form);
		if (wxConfigs == null || wxConfigs.isEmpty()) {
			return null;
		} else {
			return wxConfigs.get(0);
		}
	}

}
